#pragma once

#include <iostream>
#include <cstdlib>
#include <functional>
#include <strings.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "Log.hpp"

using callback_t = std::function<std::string \
        (const std::string &word, const std::string &whoip, uint16_t whoport)>;

static const int g_socket_fd = -1;

class DictServer
{
public:
    DictServer(uint16_t port, callback_t cb)
        : _port(port),
          _socket_fd(g_socket_fd),
          _is_running(false),
          _cb(cb)
    {
    }
    void Init()
    {
        // 1、创建套接字
        _socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_socket_fd < 0)
        {
            LOG(LogLevel::ERROR) << "create socket error";
            exit(1);
        }
        LOG(LogLevel::INFO) << "create socket success : " << _socket_fd;

        // 2、绑定
        // 2.1 给socket填充ip和端口号
        struct sockaddr_in local;
        //将local结构体内容全部清零
        bzero(&local, sizeof(local));
        local.sin_family = AF_INET;
        //ip和port也要发送给对方主机, 所以就要主机序列转网络序列
        //2.1.1. port转换
        local.sin_port = htons(_port);
        //2.1.2. ip转换
        //inet_addr的作用:
        //1、字符串转整数
        //2、主机序列转网络序列
        local.sin_addr.s_addr = INADDR_ANY;//任意IPbind
        // local.sin_addr.s_addr = htonl(INADDR_ANY);

        // local.sin_addr.s_addr = inet_addr(_ip.c_str());

        // 2.2 socket和file进行bind
        int n = bind(_socket_fd, (struct sockaddr*)&local, sizeof(local));
        if(n < 0)
        {
            LOG(LogLevel::ERROR) << "bind socket error";
            exit(2);
        }
        LOG(LogLevel::INFO) << "bind socket success : " << _socket_fd;

    }
    void Start()
    {
        //所有的服务器都是死循环
        _is_running = true;
        while(_is_running)
        {
            char buffer[1024];
            buffer[0] = 0;
            struct sockaddr_in peer;//数据发送方的socket数据
            socklen_t len = sizeof(peer);

            //1、读取数据
            //消息内容 + 发送主体
            ssize_t n = recvfrom(_socket_fd, buffer, sizeof(buffer), 0,
             (struct sockaddr*)&peer, &len);
            if(n > 0)
            {
                buffer[n] = 0;//重点
                //获取client的socket
                uint16_t clientport = ntohs(peer.sin_port);
                std::string clientip = inet_ntoa(peer.sin_addr);//四字节整数转字符

                std::string word = buffer;
                LOG(LogLevel::DEBUG) << "用户查找" << word;
                //回调
                std::string result = _cb(word, clientip, clientport);//执行任务

                //写回发送方
                sendto(_socket_fd, result.c_str(), sizeof(result), 0,
                 (struct sockaddr*)&peer, len);
                
            }
        }
        _is_running = false;  
    }
    void Stop()
    {
        _is_running = false;
    }
    ~DictServer()
    {
    }

private:
    int _socket_fd;  // 文件描述符做返回值
    //服务端不建议bindIP。服务器要接受多个客户端的IP
    // std::string _ip; // ip: 127.0.0.1(点分十进制,方便网络发送)
    callback_t _cb;//服务器的工作
    uint16_t _port;  // 端口号
    bool _is_running;
};